#include"queue.h"

//初始化
void InitQueue(Queue* queue) {
	//开辟空间
	queue->base = (ElemType*)malloc(sizeof(ElemType)*MAXSIZE);
	assert(queue->base != NULL);
	queue->front = queue->rear = 0;//初始化时队头和队尾都在0位置
}

//入队
void enQueue(Queue* queue, ElemType x) {
	//判断队列是否有空间
	if (queue->rear >= MAXSIZE)
		return;
	queue->base[queue->rear++] = x;
}

//出队
void deQueue(Queue* queue) {
	if (queue->front == queue->rear)
		return;
	queue->front++;
	//queue->rear--;
}


//打印队列数据
void showQueue(Queue* queue) {
	for (int i = queue->front; i < queue->rear; ++i) {
		cout << queue->base[i] << " ";
	}
	cout << endl;
}

//获取队头元素
void getQueueFront(Queue* queue,ElemType * v) {
	if (queue->front == queue->rear)
		return;
	*v = queue->base[queue->front];
}

//获取队列长度
int getQueueLength(Queue* queue) {
	return (queue->rear - queue->front);
}

//清空队列
void clearQueue(Queue* queue) {
	queue->front = queue->rear = 0;
}

//销毁
void destroyQueue(Queue* queue) {
	free(queue->base);
	queue->base = NULL;
}


void main() {
	Queue queue;
	InitQueue(&queue);

	for (int i = 1; i <= 8; ++i) {
		enQueue(&queue, i);
	}
	showQueue(&queue);
	deQueue(&queue);
	enQueue(&queue, 10);
	showQueue(&queue);
	clearQueue(&queue);
	destroyQueue(&queue);
}